package org.common.utils.rpc.generator.handler.methodbody;
import org.common.utils.rpc.core.Const;
import org.common.utils.rpc.generator.entity.GeneratorInfo;

/**
 * 对象返回值，方法体处理
 * @author zhouzhibing
 * @date 2025/11/15 18:42
 */
public class ObjectHandler extends AbstractHandler {
    public ObjectHandler() {
        super(ReturnType.OBJECT);
    }

    @Override
    public String handler(GeneratorInfo generatorInfo) {
        String returnTypeName = generatorInfo.getMethod().getReturnType().getName();
        return """
                %s
                java.util.concurrent.CompletableFuture future = rpcSenders.sendFuture(request);
                try {
                    return (%s)future.get(%dL , java.util.concurrent.TimeUnit.MILLISECONDS);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                """.formatted(getRequest(generatorInfo) , returnTypeName , Const.REQUEST_TIMEOUT);
    }
}
